<?php

namespace App\Server;

use Illuminate\Support\Facades\URL;

class Util 
{

    static public function ipGet()
    {
        if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
            return $_SERVER['HTTP_X_FORWARDED_FOR'];
        } elseif ($_SERVER['REMOTE_ADDR'] != '') {
            return $_SERVER['REMOTE_ADDR'];
        } else {
            return $_SERVER['HTTP_CLIENT_IP'];
        }
    }

     // aa_bb => aaBb, aa-bb => aaBb
     static public function camelCase($str)
     {
         return preg_replace_callback("/(?:[-_\/])([a-z])/", function ($match) {
             return strtoupper($match[1]);
         }, $str);
     }

    static public function routerCamelCase()
    {
        $match = [];
        preg_match("/api\/([\w\-\/]+)/i", URL::full(), $match);
        return self::camelCase($match[1]);
    }

    static function responseRetArr($data, $msg, $code)
    {
        return [
            'code' => $code,
            'message' => $msg,
            'result' => $data,
            'type' => 'success',
        ];
    }

    static public function saltPwd($pwd)
    {
        return md5('ZConZat4eG4HMIkthQAA' . $pwd);
    }

    static public function getUserPassword($pwd)
    {
        $bcryptInstance = new \Illuminate\Hashing\BcryptHasher;
        return $bcryptInstance->make(self::saltPwd($pwd));
    }
}